package com.tuantuan.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 统一日志输出工具类
 * 
 * 
 */
public class LogUtils {

	// 下面取log4j中两种 日志类型
	private static Log auditLogger = LogFactory.getLog("auditLogger");
	private static Log errorLogger = LogFactory.getLog("errorLogger");


	public static void debug(String message) {
		auditLogger.debug(message);
	}

	public static void debug(String message, Exception ex) {
		auditLogger.debug(message, ex);
	}

	public static void debug(Exception ex) {
		auditLogger.debug(ex);
	}

	public static void audit(String message) {
		auditLogger.info(message);
	}

	public static void audit(String message, Exception ex) {
		auditLogger.info(message, ex);
	}

	public static void audit(Exception ex) {
		auditLogger.info(ex);
	}

	public static void error(String message) {
		errorLogger.error(message);
	}

	public static void error(String message, Exception ex) {
		errorLogger.error(message, ex);
	}

	public static void error(Exception ex) {
		errorLogger.error(ex);
	}
	
	public static Log getAuditLogger()
	{
		return auditLogger;
	}

	public static String FormatStackTrace(Throwable throwable) 
	{
		if(throwable==null) return "";
		String rtn = throwable.getStackTrace().toString();
		try {
			Writer writer = new StringWriter();
			PrintWriter printWriter = new PrintWriter(writer);
			throwable.printStackTrace(printWriter);		
			printWriter.flush();
			writer.flush();
			rtn = writer.toString();
			printWriter.close();			
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception ex) {
		}
		return rtn;
	}
}
